package ch.zhaw.swp1.view.panels;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import javax.swing.BoxLayout;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;

import ch.zhaw.swp1.view.component.AttachmentBadge;
import ch.zhaw.swp1.view.panels.compose.GenericComposePanel;

/**
 * Panel to show user selected Attachments to send
 * @author Martin Trutmann
 */
public class AttachmentPanel extends JPanel implements ActionListener {

	/**
	 * Default Version 
	 */
	private static final long serialVersionUID = 1L;
	private JPanel titlePanel = new JPanel();
	private JPanel filePanel = new JPanel();
	private GenericComposePanel composePanel = null;
	
	/************************************************************************************/
	// Constructors
	/*----------------------------------------------------------------------------------*/
	/**
	 * Default constructor
	 * @param composePanel the compose panel containing this attachment panel
	 */
	public AttachmentPanel(GenericComposePanel composePanel){
		super();
		init();
		layoutComponents();
		this.composePanel = composePanel;
	}

	/************************************************************************************/
	// Methods
	/*----------------------------------------------------------------------------------*/
	/**
	 * Initialize
	 */
	private void init(){
		setLayout(new BorderLayout());
		titlePanel.setLayout(new GridLayout(0,1));
		filePanel.setLayout(new BoxLayout(filePanel, BoxLayout.Y_AXIS));
		filePanel.setAlignmentY(TOP_ALIGNMENT);
	}
	
	/**
	 * Lay out components on list
	 */
	private void layoutComponents(){
		JLabel title = new JLabel("Attachments:");
		title.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 16));
		title.setBackground(Color.LIGHT_GRAY);
		title.setOpaque(true);
		titlePanel.add(title);
		
		add(titlePanel, BorderLayout.NORTH);
		add(filePanel, BorderLayout.CENTER);
	}
	

	/**
	 * Add attachment to be displayed on screen
	 * @param file File representing the attachment
	 */
	public void addAttachment(File file){
		AttachmentBadge badge = new AttachmentBadge(file);
		badge.getBadgeButton().addActionListener(this);
		badge.setAlignmentX(Component.CENTER_ALIGNMENT);
		badge.setAlignmentY(Component.TOP_ALIGNMENT);
		badge.setBackground(new Color(224, 224, 224));
		badge.setOpaque(true);
		
		filePanel.add(badge);
		filePanel.revalidate();
	}

	
	/**
	 * Reset Pane removing all badges from list
	 */
	public void reset(){
		filePanel.removeAll();
		repaint();
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		JComponent button = (JComponent) e.getSource();
		
		AttachmentBadge badge = (AttachmentBadge) button.getParent();
		filePanel.remove(badge);
		filePanel.revalidate();
		composePanel.getAttachments().remove(badge.getFile());
		composePanel.revalidate();
		revalidate();
		repaint();
	}

}
